package com.atjava.webspider.util;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

import com.atjava.jox.convert.Converter;
import com.atjava.jox.convert.XmlDefaultConverter;

public class TableUtil {

	public static List<String[]> getTableData(List<?> dataList,String ... props)throws Exception{
		List<String[]> data = new ArrayList<String[]>();
		if(dataList == null)
			return data;
		for (int i = 0; i < dataList.size(); i++) {
			data.add(getRow(dataList.get(i),props));
		}
		return data;
	}
	
	public static String[] getRow(Object obj,String ... props)throws Exception{
		Converter converter = new XmlDefaultConverter();
		String [] row = new String [props.length];
		for (int i = 0; i < row.length; i++) {
			Object value = ReflectUtil.getValue(obj, props[i]);
			if(value == null)
				row[i] = "";
			else
				row[i] = converter.convert(value);
		}
		return row;
	}
	
	public String[] getRow(Object obj)throws Exception{
		return getRow(obj,new String[0]);
	}
	
	/*
	public boolean isInArray(String s,String [] sary){
		if(sary == null || sary.length == 0)
			return false;
		for (int i = 0; i < sary.length; i++) {
			if(s.equals(sary[i]))
				return true;
		}
		return false;
	}*/
	//自动设置列宽   
    public static void fitTableColumns(JTable table) {   
        JTableHeader header = table.getTableHeader();   
        int rowCount = table.getRowCount();   
        Enumeration<?> columns = table.getColumnModel().getColumns();   
        while (columns.hasMoreElements()) {   
            TableColumn column = (TableColumn) columns.nextElement();   
            int col = header.getColumnModel().getColumnIndex(column.getIdentifier());   
            int width = (int) table.getTableHeader().getDefaultRenderer()   
                    .getTableCellRendererComponent(table,column.getIdentifier(), false, false, -1, col).getPreferredSize().getWidth();   
            for (int row = 0; row < rowCount; row++) {   
                int preferedWidth = (int) table.getCellRenderer(row, col)   
                        .getTableCellRendererComponent(table,   
                                table.getValueAt(row, col), false, false, row,col).getPreferredSize().getWidth();   
                width = Math.max(width, preferedWidth);   
            }   
            header.setResizingColumn(column); // 此行很重要   
            column.setWidth(width + 10 + table.getIntercellSpacing().width);   
        }   
    }   
  
    //手动设置列宽   
    public static void fitTableColumns(JTable table, int[] columnWidths) {   
        for (int i = 0; i < columnWidths.length; i++) {   
            table.getColumnModel().getColumn(i).setPreferredWidth(   
                    columnWidths[i]);   
        }   
    }
    
    public static void setTableAlign(JTable table,int align){
    	DefaultTableCellRenderer headerRender = 
			(DefaultTableCellRenderer)table.getTableHeader().getDefaultRenderer();
		headerRender.setHorizontalAlignment(align);
		DefaultTableCellRenderer render = 
			(DefaultTableCellRenderer)table.getDefaultRenderer(Object.class);
		render.setHorizontalAlignment(align);
    }

}
